<?php
class Order_Shensu 
{
    public function __construct ($orderId = NULL)
    {
        $this->_orderId = $orderId;
    }

    public function detail ($orderId = NULL)
    {
        $orderId = $orderId > 0 ? $orderId : $this->_orderId;
        $current = DB::select()
            ->from('app_order_shensu')
            ->where('orderId', '=', $orderId)
            ->limit(1)
            ->execute()
            ->current();

        return $current;
    }

    public function create ($orderId = NULL)
    {
        $orderId = $orderId > 0 ? $orderId : $this->_orderId;

        $shensuId = DB::select('id')
            ->from('app_order_shensu')
            ->where('orderId', '=', $orderId)
            ->execute()
            ->get('id');

        // 申诉已存在
        if ($shensuId > 0)
        {
            DB::update('app_order_shensu')
                ->set(array('shensuStatus' => '未处理'))
                ->where('id', '=', $shensuId)
                ->execute();

            return $shensuId;
        }
        // 添加申诉处理请求
        else
        {
            list($insertId, $rows) = DB::insert('app_order_shensu')
                ->columns (array('orderId', 'shensuStatus', 'handleTime', 'shensuCreated'))
                ->values (array($orderId, '未处理', NULL, DB::expr('unix_timestamp(now())')))
                ->execute();

            return $insertId;
        }
    }

    public function setStatus ($orderNo, $status, $comments)
    {
        $orderId = Order::getInternalOrderId($orderNo); 
        
        $rows = DB::update('app_order_shensu')
            ->set(
                array(
                    'shensuStatus' => $status,
                    'handleTime'   => DB::expr('unix_timestamp(now())'),
                    'comments'     => $comments,
                ))
            ->where('orderId', '=', $orderId)
            ->execute();

        if ($rows > 0 && $status == '已处理')
        {
            Order::resetStatus($orderNo, 32);
        }

        return $rows > 0;
    }

	// 申诉列表
	// listWheres array 订单查询条件
    public function recordsList ($offset, $limit, $listWheres = array())
    {
        $orders = array();
        $count = DB::select(array('count("a.id")', 'counts'))
            ->from(array('app_order', 'a'))
            ->join(array('app_order_shensu', 'b'), 'LEFT')
            ->on('a.id', '=', 'b.orderId')
            ->where_open()
            ->where('a.status', 'in', array(31, 32))
            ->or_where('b.orderId', 'is not', NULL)
            ->where_close();

        $query = DB::select('a.*', 'b.shensuStatus', 'b.handleTime', 'b.shensuCreated')
            ->from(array('app_order', 'a'))
            ->join(array('app_order_shensu', 'b'), 'LEFT')
            ->on('a.id', '=', 'b.orderId')
            ->where_open()
            ->where('a.status', 'in', array(31, 32))
            ->or_where('b.orderId', 'is not', NULL)
            ->where_close()
            ->order_by('a.paidTime', 'desc');

        foreach ($listWheres as $rs)
        {
            if (is_array($rs))
            {
                $query->where($rs[0], $rs[1], $rs[2]);
                $count->where($rs[0], $rs[1], $rs[2]);
            }
        }

        $count = $count->execute()->get('counts');
        $query = $query->offset($offset)->limit($limit)->execute();

        foreach ($query as $rs)
        {
            $rs['statusName']  = Order::orderStatusName ($rs['status']);
            $rs['title'] = utf8::substr($rs['title'], 0, 50) . '...';
            $rs['shortTitle'] = utf8::substr($rs['title'], 0, 15) . '...';

            $orders[] = $rs;
        }

        return array($count, $orders);
    }
}
